<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\wechat\controller\admin;

use app\wechat\common\AdminWxController;
use app\admin\common\AdminException;

class User extends AdminWxController
{
    public function index()
    {
        $data = $this->_get_pagination_data();

        $data['where_list']['mp_number'] = $this->mp_number;

        $data['total_rows'] = model('wechat/user')->query_count($data['where_list']);
        $data['page_count'] = intval(ceil($data['total_rows'] / $data['page_size']));
        $data['page_num'] = $data['page_num'] > $data['page_count'] ? 1 : $data['page_num'];

        $data['user_list'] = model('wechat/user')->query_list($data['where_list'], $data['page_num'], $data['page_size'], $data['sort_field'], $data['sort_order']);

        $this->display('admin/user/user_list', $data);
    }

    public function create()
    {
        $user_info = model('wechat/user')->initDefault();

        if (request()->isPost()) {
            $user_info->acceptData($this->post_array());

            try {
                service('wechat/user')->add_user($this->mp_number, $user_info['openid']);

                $user_info->save();
            } catch (AdminException $ae) {
                $this->error($ae->getString());
            }

            $this->success('新增成功。', $this->url('index'));
        }

        $data['user_info'] = $user_info;
        $this->display('admin/user/user_form', $data);
    }

    public function modify($id)
    {
        $user_info = model('wechat/user')->findOrFail($id);
        $old_openid = $user_info['openid'];

        if (request()->isPost()) {
            $user_info->acceptData($this->post_array());

            try {
                if ($user_info['openid'] != $old_openid) {
                    service('wechat/user')->remove_user($this->mp_number, $old_openid);
                    service('wechat/user')->add_user($this->mp_number, $user_info['openid']);
                }

                $user_info->save();
            } catch (AdminException $ae) {
                $this->error($ae->getString());
            }

            $this->success('修改成功。', $this->url('index'));
        }

        $data['user_info'] = $user_info;
        $this->display('admin/user/user_form', $data);
    }

    public function view_user_info($id)
    {
        $user_info = model('wechat/user')->findOrFail($id);

        $data['user_info'] = $user_info;
        $this->display('admin/user/user_view', $data);
    }

    public function bulk_delete($ids = array())
    {
        $user_list = model('wechat/user')->where('id', 'in', $ids)->select();

        foreach ($user_list as $user_info) {
            try {
                service('wechat/user')->remove_user($this->mp_number, $user_info['openid']);

                $user_info->delete();
            } catch (AdminException $ae) {
                $this->error($ae->getString());
            }
        }

        $this->success('删除成功。', $this->url('index'));
    }

    public function download()
    {
        set_time_limit(0);

        try {
            service('wechat/user')->download_user_list($this->mp_number);
        } catch (AdminException $ae) {
            $this->error($ae->getString());
        }

        $this->success('下载用户数据成功。', $this->url('index'));
    }

    public function update_user_info($ids = array())
    {
        set_time_limit(0);

        $user_list = model('wechat/user')->where('id', 'in', $ids)->select();

        foreach ($user_list as $user_info) {
            try {
                service('wechat/user')->update_user_info($this->mp_number, $user_info['openid']);
            } catch (AdminException $ae) {
                $this->error($ae->getString());
            }
        }

        $this->success('更新用户信息成功。', $this->url('index'));
    }

    public function add_blacklist($ids = array())
    {
        try {
            set_time_limit(0);

            $where_map['mp_number'] = array('eq', $this->mp_number);
            $where_map['id'] = array('in', $ids);
            $where_map['blacklist'] = array('eq', 0);
            $openid_list = model('wechat/user')->where($where_map)->column('openid');

            if ($openid_list) {
                service('wechat/blacklist')->add_blacklist($this->mp_number, $openid_list);
            }
        } catch (AdminException $ae) {
            $this->error($ae->getString());
        }

        $this->success('更新黑名单成功。', $this->url('index'));
    }

    public function remove_blacklist($ids = array())
    {
        try {
            set_time_limit(0);

            $where_map['mp_number'] = array('eq', $this->mp_number);
            $where_map['id'] = array('in', $ids);
            $where_map['blacklist'] = array('eq', 1);
            $openid_list = model('wechat/user')->where($where_map)->column('openid');

            if ($openid_list) {
                service('wechat/blacklist')->remove_blacklist($this->mp_number, $openid_list);
            }
        } catch (AdminException $ae) {
            $this->error($ae->getString());
        }

        $this->success('更新黑名单成功。', $this->url('index'));
    }
}